/*******************************************************************************
 * Copyright (c) 2012, 2013 XML-Print Project.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     XML-Print Project - initial API and implementation
 ******************************************************************************/
package info.xmlprint.action;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.wizards.newresource.BasicNewFileResourceWizard;

public class NewXml extends AbstractHandler {

	private class NewWizard extends BasicNewFileResourceWizard { 
		

	    /* (non-Javadoc)
	     * Method declared on IWizard.
	     */
	    public void addPages() {
	    	super.addPages();
	        WizardNewFileCreationPage  mainPage=(WizardNewFileCreationPage) getPages()[0];
	        mainPage.setFileExtension("xml");   
	    }

	    
	    
		public void init(IWorkbench workbench,
				IStructuredSelection currentSelection) {
			super.init(workbench, currentSelection);
		}
	}

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		ISelection selection = HandlerUtil.getCurrentSelection(event);
		if (!(selection instanceof IStructuredSelection)) {
			return null;
		}
		IStructuredSelection sel = (IStructuredSelection) selection;

		NewWizard wizard = new NewWizard();
		wizard.init(PlatformUI.getWorkbench(), sel);
		WizardDialog dialog = new WizardDialog(null, wizard);

		Object result = null;
		if (WizardDialog.OK == dialog.open()) {
			result = wizard.getContainer();
		}

		return result;
	}

}
